<?php
/**
 * CompleXml Framework
 *
 * @category   CompleXml
 * @package    CompleXml_Core
 * @copyright  Copyright (c) 2008 Andrey Kucherenko
 * @license    New BSD License
 * @version    $Id: Core.php 10 2008-11-17 16:10:16Z kucherenko.andrey $
 */

final class CompleXml_Core
{
	static public function start (CompleXml_Object_Abstract $Object)
	{
		$handlers = (array) $Object->getHandlers();

		$listeners = array();

		$outputs = array();

		foreach ($handlers as $handler) {
			if ($handler instanceof CompleXml_Handler_Input_Abstract ){
				$handler->execute($Object);
			}elseif ($handler instanceof CompleXml_Handler_Listener_Abstract){
				$listeners[] = $handler;
			}elseif ($handler instanceof CompleXml_Handler_Output_Abstract ){
				$outputs[] = $handler;
			}
		}


		foreach ($listeners as $listener) {
			$listener->execute($Object);
		}

		$actions = (array) $Object->getActions();

		while (list( $method, $status) = each($actions)) {
			if ($status && method_exists($Object, $method)){
				$Object->$method();
				$Object->deleteAction($method);
			}
		}

		foreach ($outputs as $output) {
			$output->execute($Object);
		}
	}

	/**
	 * autoload
	 *
	 * simple autoload function
	 * returns true if the class was loaded, otherwise false
	 *
	 * @param string $classname
	 * @return boolean
	 */
	public static function autoload($className)
	{
		if (class_exists($className, false) || interface_exists($className, false)) {
			return false;
		}

		$class = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

		if (file_exists($class)) {
			require $class;
			return true;
		}
		return false;
	}
}